package training_camp.day1.leetcode69;

class Solution {
    public int mySqrt(int x) {
        int l = 0;
        int r = x;
        while (l <= r) {
            int mid = l + (r - l) / 2;
            if ((long)mid * mid < x) {
                l = mid + 1;
            } else if ((long)mid * mid > x) {
                r = mid - 1;
            } else {
                r = mid - 1;
            }
        }
        if ((long)(r+1)*(r+1)== x){
            return r+1;
        }
        return r;
    }
}